<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Session;
/**
 * 资产信息
 *
 * @icon fa fa-circle-o
 */
class Property extends Backend
{
    
    /**
     * Property模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Property');
        $this->view->assign("propertyStatusList", $this->model->getPropertyStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
     /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a"); 
            if ($params)
            {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill)
                {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try
                {
                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }

                    //添加额外参数
                    $params["user_id"] = Session::get("admin.id");
                    // $params["building_add_user_name"] = Session::get("admin.username");
                    $params["property_add_time"] = time();

                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($this->model->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("property_large_category", $this->getDictionary('30000')); 
        $this->view->assign("warehouse", $this->getDictionary('40000'));
        return $this->view->fetch();
    }

    public function getDictionary($parent_code){
        $warehouse = model('Dictionary')->where(['parent_code'=>$parent_code])->select();
        $t =  array();
        if ($this->request->isAjax()){
            foreach ($warehouse as $key => $value) {
                $t[$key]['code'] = $value['code'];
                $t[$key]['name'] = $value['name'];
            } 
            echo json_encode($t);
            return ;
        }else{
            foreach ($warehouse as $key => $value) {
                $t[$value['code']] = $value['name'];
            } 
            return $t;
        } 
    }

}
